package com.example.demo.service.impl;

import com.example.demo.dto.ChatMessageDTO;
import com.example.demo.entity.ChatMessage;
import com.example.demo.exception.ChatMessageNotFound;
import com.example.demo.mapper.ChatMessageMapper;
import com.example.demo.service.ChatMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatMessageServiceImpl implements ChatMessageService {

    private final ChatMessageMapper chatMessageMapper;
    @Autowired
    public ChatMessageServiceImpl(ChatMessageMapper chatMessageMapper) {
        this.chatMessageMapper = chatMessageMapper;
    }

    @Override
    public List<ChatMessageDTO> getAllMessageByUserId(String userId) throws ChatMessageNotFound {
        List<ChatMessageDTO> chatMessages = chatMessageMapper.selectMessagesByUserId(userId);
        if (chatMessages == null || chatMessages.isEmpty()) {
            throw new ChatMessageNotFound("暂时没有用户id为" + userId + "的消息！");
        }
        return chatMessages;
    }

    @Override
    public boolean addChatMessage(ChatMessage chatMessage) {
        int affectedRows = chatMessageMapper.insertMessage(chatMessage);
        return affectedRows == 1;
    }
}
